﻿using DataAccessLayer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utilities;

namespace BussinessLogicLayer
{
    public class GiaiDauBLL
    {
        static QuanLyGiaiDauDataContext QLGD = new QuanLyGiaiDauDataContext();
        public static List<GiaiDau> LayGiaiDau()
        {
            var giaiDau = from gd in QLGD.GiaiDaus
                          where gd.TrangThai == "Bắt đầu"
                          select gd;
            return giaiDau.ToList();
        }
        public static List<GiaiDau> LayTatCaGD()
        {
            var GiaiDau = from gd in QLGD.GiaiDaus
                          where gd.TrangThai != "Đã xóa"
                          select gd;
            return GiaiDau.ToList();
        }
        public static bool ThemGiaiDau(GiaiDau entity)
        {
            try
            {
                GiaiDau GD = new GiaiDau();
                GD.TenGiaiDau = entity.TenGiaiDau;
                GD.NgayBatDau = entity.NgayBatDau;
                GD.NgayKetThuc = entity.NgayKetThuc;
                GD.MoTa = entity.MoTa;
                GD.TrangThai = entity.TrangThai;
                QLGD.GiaiDaus.InsertOnSubmit(GD);
                QLGD.SubmitChanges();
                int kq = 1;
                return kq > 0;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return false;
            }
        }
        public static bool CapNhatGiaiDau(GiaiDau entity)
        {
            try
            {
                GiaiDau CapNhatGD = (from gd in QLGD.GiaiDaus
                                     select gd).Single(gdau => gdau.MaGiaiDau == entity.MaGiaiDau);
                CapNhatGD.TenGiaiDau = entity.TenGiaiDau;
                CapNhatGD.NgayBatDau = entity.NgayBatDau;
                CapNhatGD.NgayKetThuc = entity.NgayKetThuc;
                CapNhatGD.MoTa = entity.MoTa;
                CapNhatGD.TrangThai = entity.TrangThai;
                QLGD.SubmitChanges();
                int kq = 1;
                return kq > 0;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return false;
            }
        }
        public static bool XoaGiaiDau(int MaGD)
        {
            try
            {
                GiaiDau XoaGD = (from gd in QLGD.GiaiDaus
                                 select gd).Single(gdau => gdau.MaGiaiDau == MaGD);
                XoaGD.TrangThai = "Đã xóa";
                QLGD.SubmitChanges();
                int kq = 1;
                return kq > 0;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return false;
            }

        }
    }
}
